package com.utooo.service.supplier;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.utooo.constant.ResultCodeConstant;
import com.utooo.dao.purchase.ContractAcceptanceMapper;
import com.utooo.dao.purchase.SupplierContractAcceptanceMapper;
import com.utooo.pojo.domain.purchase.contract.ContractAcceptanceDO;
import com.utooo.pojo.domain.supplier.settlement.SupplierContractAcceptanceDO;
import com.utooo.pojo.dto.common.RestResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@RequiredArgsConstructor
public class SupplierContractAcceptanceServiceImpl extends ServiceImpl<SupplierContractAcceptanceMapper, SupplierContractAcceptanceDO>
        implements SupplierContractAcceptanceService {

    private final ContractAcceptanceMapper contractAcceptanceMapper;
    @Override
    public RestResult<String> confirm(String id) {
        SupplierContractAcceptanceDO contractAcceptanceDO = this.getById(id);
        if (contractAcceptanceDO == null) {
            log.error("供货方合同验收信息不存在！");
            return new RestResult<>(ResultCodeConstant.CODE_400, "供货方合同验收信息不存在！");
        }
        String contractAcceptanceId = contractAcceptanceDO.getContractAcceptanceId();
        ContractAcceptanceDO contractAcceptance = contractAcceptanceMapper.selectById(contractAcceptanceId);
        if (contractAcceptance == null) {
            log.error("采购方合同验收信息不存在！");
            return new RestResult<>(ResultCodeConstant.CODE_400, "采购方合同验收信息不存在！");
        }
        contractAcceptance.setStatus("3");
        contractAcceptanceMapper.updateById(contractAcceptance);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult<String> revoke(String id) {
        SupplierContractAcceptanceDO contractAcceptanceDO = this.getById(id);
        if (contractAcceptanceDO == null) {
            log.error("供货方合同验收信息不存在！");
            return new RestResult<>(ResultCodeConstant.CODE_400, "供货方合同验收信息不存在！");
        }
        String contractAcceptanceId = contractAcceptanceDO.getContractAcceptanceId();
        ContractAcceptanceDO contractAcceptance = contractAcceptanceMapper.selectById(contractAcceptanceId);
        if (contractAcceptance == null) {
            log.error("采购方合同验收信息不存在！");
            return new RestResult<>(ResultCodeConstant.CODE_400, "采购方合同验收信息不存在！");
        }
        this.removeById(id);
        contractAcceptance.setStatus("4");
        contractAcceptanceMapper.updateById(contractAcceptance);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG);
    }
}